<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>中国联通 | 智能模型调用监控平台</title>

    <!-- 引入 Bootstrap 5 -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入 FontAwesome 图标 -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <!-- 引入 ECharts -->
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script>

    <style>
        :root {
            --unicom-red: #E60027; /* 联通标准红 */
            --unicom-dark: #1f1f1f;
            --bg-color: #f4f7fc;
            --card-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
        }

        body {
            background-color: var(--bg-color);
            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
            color: #333;
        }

        /* 顶部导航栏 */
        .navbar {
            background-color: #fff;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            padding: 1rem 2rem;
        }
        .brand-logo {
            font-weight: 800;
            font-size: 1.5rem;
            color: #000;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .brand-red { color: var(--unicom-red); }

        /* 卡片样式优化 */
        .stat-card {
            background: #fff;
            border-radius: 12px;
            border: none;
            box-shadow: var(--card-shadow);
            transition: transform 0.2s;
            height: 100%;
            position: relative;
            overflow: hidden;
        }
        .stat-card:hover {
            transform: translateY(-5px);
        }
        .stat-card::before {
            content: "";
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            width: 4px;
            background: var(--unicom-red);
            opacity: 0;
            transition: opacity 0.3s;
        }
        .stat-card:hover::before { opacity: 1; }

        .card-icon {
            width: 48px;
            height: 48px;
            background-color: rgba(230, 0, 39, 0.1);
            color: var(--unicom-red);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.2rem;
        }
        .stat-value {
            font-size: 2rem;
            font-weight: 700;
            color: var(--unicom-dark);
            margin-top: 10px;
        }
        .stat-label {
            color: #888;
            font-size: 0.9rem;
        }

        /* 图表容器 */
        .chart-box {
            background: #fff;
            border-radius: 12px;
            padding: 20px;
            box-shadow: var(--card-shadow);
            margin-bottom: 20px;
            min-height: 400px;
        }
        .chart-title {
            font-weight: 700;
            margin-bottom: 20px;
            border-left: 4px solid var(--unicom-red);
            padding-left: 10px;
            font-size: 1.1rem;
        }
        .chart-container {
            width: 100%;
            height: 320px;
        }

        /* 按钮样式 */
        .btn-unicom {
            background-color: var(--unicom-red);
            color: #fff;
            border-radius: 50px;
            padding: 8px 25px;
            font-weight: 600;
            border: none;
            box-shadow: 0 4px 10px rgba(230, 0, 39, 0.3);
        }
        .btn-unicom:hover {
            background-color: #c4001f;
            color: #fff;
        }
    </style>
</head>
<body>

<!-- 导航栏 -->
<nav class="navbar navbar-expand-lg sticky-top">
    <div class="container-fluid">
        <div class="brand-logo">
            <!-- 模拟联通中国结Logo -->
            <span style="font-size: 1.8rem;">🔴</span>
            <span>China Unicom <span class="brand-red">中国联通</span></span>
            <span style="font-size: 1rem; color: #999; margin-left: 10px; font-weight: 400;">| 智能模型调用监控平台</span>
        </div>
        <button class="btn btn-unicom" onclick="simulateCall()">
            <i class="fas fa-sync-alt me-2"></i>模拟调用测试
        </button>
    </div>
</nav>

<div class="container-fluid p-4">
    <!-- 核心指标行 -->
    <div class="row g-4 mb-4">
        <!-- 总调用量 -->
        <div class="col-md-3">
            <div class="stat-card p-4 d-flex align-items-start justify-content-between">
                <div>
                    <div class="stat-label">平台总调用量 (Total)</div>
                    <div class="stat-value" id="total-calls">--</div>
                    <div class="text-success small mt-2"><i class="fas fa-arrow-up"></i> 持续增长</div>
                </div>
                <div class="card-icon">
                    <i class="fas fa-database"></i>
                </div>
            </div>
        </div>
        <!-- 活跃模型 -->
        <div class="col-md-3">
            <div class="stat-card p-4 d-flex align-items-start justify-content-between">
                <div>
                    <div class="stat-label">接入模型数 (Models)</div>
                    <div class="stat-value" id="active-models">--</div>
                    <div class="text-muted small mt-2">多模型路由支持</div>
                </div>
                <div class="card-icon">
                    <i class="fas fa-robot"></i>
                </div>
            </div>
        </div>
        <!-- 接入用户 -->
        <div class="col-md-3">
            <div class="stat-card p-4 d-flex align-items-start justify-content-between">
                <div>
                    <div class="stat-label">活跃用户 (Active Users)</div>
                    <div class="stat-value" id="active-users">--</div>
                    <div class="text-primary small mt-2">企业级租户</div>
                </div>
                <div class="card-icon">
                    <i class="fas fa-users"></i>
                </div>
            </div>
        </div>
        <!-- 模拟QPS -->
        <div class="col-md-3">
            <div class="stat-card p-4 d-flex align-items-start justify-content-between">
                <div>
                    <div class="stat-label">系统健康度 (Health)</div>
                    <div class="stat-value" style="color: #198754;">100%</div>
                    <div class="text-muted small mt-2">服务运行正常</div>
                </div>
                <div class="card-icon" style="background-color: rgba(25, 135, 84, 0.1); color: #198754;">
                    <i class="fas fa-heartbeat"></i>
                </div>
            </div>
        </div>
    </div>

    <!-- 图表区域 -->
    <div class="row g-4">
        <!-- 左侧：每日趋势 -->
        <div class="col-lg-8">
            <div class="chart-box">
                <div class="chart-title">近7日调用量趋势分析</div>
                <div id="chart-daily" class="chart-container"></div>
            </div>
        </div>

        <!-- 右侧：模型占比 -->
        <div class="col-lg-4">
            <div class="chart-box">
                <div class="chart-title">模型消耗占比</div>
                <div id="chart-model" class="chart-container"></div>
            </div>
        </div>
    </div>

    <!-- 底部：用户排名 -->
    <div class="row g-4 mt-1">
        <div class="col-12">
            <div class="chart-box">
                <div class="chart-title">各部门/用户调用量排名 (Top Users)</div>
                <div id="chart-user" class="chart-container" style="height: 280px;"></div>
            </div>
        </div>
    </div>
</div>

<footer class="text-center py-4 text-muted small">
    &copy; 2023 China Unicom Data Services. Internal Use Only.
</footer>

<script>
    // 联通配色方案
    const colorPalette = ['#E60027', '#F4811F', '#00A0E9', '#555555', '#80BD01'];

    // 初始化 ECharts
    const dailyChart = echarts.init(document.getElementById('chart-daily'));
    const modelChart = echarts.init(document.getElementById('chart-model'));
    const userChart = echarts.init(document.getElementById('chart-user'));

    // 通用图表配置 (为了保持风格统一)
    const commonOption = {
        grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
        textStyle: { fontFamily: 'Arial, sans-serif' }
    };

    async function fetchData() {
        try {
            const response = await fetch('/api/stats');
            const data = await response.json();

            // 1. 更新指标卡片
            // 添加数字滚动效果（可选，这里直接更新）
            document.getElementById('total-calls').innerText = data.total_calls.toLocaleString();
            document.getElementById('active-models').innerText = data.model_stats.length;
            document.getElementById('active-users').innerText = data.user_stats.categories.length;

            // 2. 渲染折线图 (高级渐变效果)
            dailyChart.setOption({
                color: ['#E60027'],
                tooltip: {
                    trigger: 'axis',
                    backgroundColor: 'rgba(255,255,255,0.9)',
                    borderColor: '#E60027',
                    textStyle: { color: '#333' }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: data.daily_stats.dates,
                    axisLine: { lineStyle: { color: '#ccc' } }
                },
                yAxis: {
                    type: 'value',
                    splitLine: { lineStyle: { type: 'dashed', color: '#eee' } }
                },
                series: [{
                    name: '调用次数',
                    data: data.daily_stats.values,
                    type: 'line',
                    smooth: true, // 平滑曲线
                    symbol: 'circle',
                    symbolSize: 8,
                    lineStyle: { width: 3, shadowColor: 'rgba(230,0,39,0.3)', shadowBlur: 10 },
                    areaStyle: {
                        // 线性渐变填充
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            { offset: 0, color: 'rgba(230, 0, 39, 0.4)' },
                            { offset: 1, color: 'rgba(230, 0, 39, 0.05)' }
                        ])
                    }
                }]
            });

            // 3. 渲染环形图 (Donut Chart)
            modelChart.setOption({
                color: colorPalette,
                tooltip: { trigger: 'item' },
                legend: { bottom: '0%', left: 'center', icon: 'circle' },
                series: [{
                    name: '模型分布',
                    type: 'pie',
                    radius: ['40%', '70%'], // 环形
                    avoidLabelOverlap: false,
                    itemStyle: {
                        borderRadius: 5,
                        borderColor: '#fff',
                        borderWidth: 2
                    },
                    label: { show: false, position: 'center' },
                    emphasis: {
                        label: {
                            show: true,
                            fontSize: 18,
                            fontWeight: 'bold',
                            formatter: '{b}\n{d}%'
                        },
                        itemStyle: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.2)' }
                    },
                    data: data.model_stats
                }]
            });

            // 4. 渲染柱状图
            userChart.setOption({
                tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' } },
                grid: { top: '10%', bottom: '15%', left: '5%', right: '5%' },
                xAxis: {
                    type: 'category',
                    data: data.user_stats.categories,
                    axisTick: { show: false },
                    axisLine: { lineStyle: { color: '#ccc' } }
                },
                yAxis: { show: false }, // 隐藏Y轴，让数据更干净
                series: [{
                    name: '调用量',
                    data: data.user_stats.values,
                    type: 'bar',
                    barWidth: '40%',
                    label: { show: true, position: 'top', color: '#666' },
                    itemStyle: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            { offset: 0, color: '#F4811F' }, // 联通橙
                            { offset: 1, color: '#E60027' }  // 联通红
                        ]),
                        borderRadius: [5, 5, 0, 0]
                    }
                }]
            });

        } catch (error) {
            console.error('Error:', error);
        }
    }

    async function simulateCall() {
        const btn = document.querySelector('.btn-unicom i');
        btn.classList.add('fa-spin'); // 添加旋转动画
        await fetch('/api/add_call');
        await fetchData();
        setTimeout(() => btn.classList.remove('fa-spin'), 500); // 停止动画
    }

    fetchData();
    window.addEventListener('resize', function() {
        dailyChart.resize();
        modelChart.resize();
        userChart.resize();
    });
</script>
</body>
</html>
